package com.hyl.icon.controller;

import com.hyl.icon.common.BaseResponse;
import com.hyl.icon.common.ErrorCode;
import com.hyl.icon.common.ResultUtils;
import com.hyl.icon.exception.BusinessException;
import com.hyl.icon.model.dto.message.MessageAddRequest;
import com.hyl.icon.model.entity.Message;
import com.hyl.icon.service.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/chat")
@Slf4j
public class MessageController {

    @Resource
    private MessageService messageService;
    //TODO 每个人只能查看自己的信息列表，管理员可以查看所有人的

    /**
     * 取用户发送给他人的信息
     * @param userId
     * @return
     */
    @GetMapping("/messages/send")
    public BaseResponse<Map<Long,List<Message>>> getSendMessages(long userId) {

        return ResultUtils.success(messageService.getSendMessages(userId));
    }

    /**
     * 取用户接受给他人的信息
     * @param userId
     * @return
     */
    @GetMapping("/messages/receive")
    public BaseResponse<Map<Long,List<Message>>> getReceiveMessages(long userId) {

        return ResultUtils.success(messageService.getReceiveMessages(userId));
    }

    /**
     * 发送消息
     * @param messageAddRequest
     * @return
     */
    @PostMapping("/send")
    public BaseResponse<Boolean> sendMessage(@RequestBody MessageAddRequest messageAddRequest) {
        if (messageAddRequest == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"请求参数错误");
        }

        Message message = new Message();
        BeanUtils.copyProperties(messageAddRequest,message);
        return ResultUtils.success(messageService.sendMessage(message));
    }
}
